package com.studyyoun.android.nonghe.home.activity;
/**
 * Created by zhaolong on 2017/10/5.
 * 站在顶峰，看世界
 * 落在谷底，思人生
 */

import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.androidlongs.pullrefreshrecyclerylib.common.PullRecyclerViewUtils;
import com.androidlongs.pullrefreshrecyclerylib.inter.PullRecyclerViewLinserner;
import com.androidlongs.pullrefreshrecyclerylib.inter.PullRecyclerViewOnItemClickLinserner;
import com.androidlongs.pullrefreshrecyclerylib.model.PullRecyclerMoreStatueModel;
import com.studyyoun.android.androidcommonbaselibrary.activity.CommonBaseActivity;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.callback.OkhttpCommonCallBack;
import com.studyyoun.android.androidcommonbaselibrary.okhttp.client.OkhttpCommonClient;
import com.studyyoun.android.androidcommonbaselibrary.utils.AppUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.DateUtils;
import com.studyyoun.android.androidcommonbaselibrary.utils.ToastUtils;
import com.studyyoun.android.nonghe.R;
import com.studyyoun.android.nonghe.home.model.AppVersonModel;
import com.studyyoun.android.nonghe.http.HttpUrlHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * class infation
 */
public class AppVersonActivity extends CommonBaseActivity {
    private View mBackLinearLayout;
    private TextView mHeaderTitleTextView;
    private LinearLayout mMainContentLinearLayout;

    private List<PullRecyclerMoreStatueModel> mCommonVersionModelList = new ArrayList<>();
    private PullRecyclerViewUtils mPullRecyclerViewUtils;

    @Override
    protected void getAllIntentExtraDatas(Intent intent) {

    }

    @Override
    protected int getCommonLayoutId() {
        return R.layout.activity_app_verson_layout;
    }

    @Override
    protected void commonInitView(View view) {
        mBackLinearLayout = findViewById(R.id.ll_base_back);
        mHeaderTitleTextView = findViewById(R.id.tv_base_header_title);
        mMainContentLinearLayout = findViewById(R.id.ll_verson_main_content);
        //初始化
        mPullRecyclerViewUtils = PullRecyclerViewUtils.getInstance();


        //3初始化Recyclerview
        /**
         * 参数一 this Context实例
         * 参数二 单一布局模式条目布局ID
         * 参数三 数据集合
         * 参数四 回调监听
         */
        RelativeLayout relativeLayout = mPullRecyclerViewUtils.setRecyclerViewFunction(mContext, -1, mCommonVersionModelList, mPullRecclerLinserner,mItemClickLinserner);

        //4相关设置

        //下拉刷新和上拉加载更多模式
        mPullRecyclerViewUtils.setRecyclerviewStatue(PullRecyclerViewUtils.RECYCLRYVIEW_STATUE.PULL_AND_UP);

        mPullRecyclerViewUtils.setFirstDefaultPageType(PullRecyclerViewUtils.SHOW_DEFAUTLE_PAGE_TYPE.LOADING);

        //5将recyclerview添加到当前显示的页面中
        mMainContentLinearLayout.addView(relativeLayout);
    }

    @Override
    protected void commonFunction() {

    }

    @Override
    protected void commonDelayFunction() {
        mHeaderTitleTextView.setText("版本信息概述");
        mBackLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        loadingNetDataFunction();
    }

    private int pageIndex=0;
    private int pageSize=30;
    private void loadingNetDataFunction() {
        String url= HttpUrlHelper.sGetAppVersionUrl;
        ConcurrentHashMap<String,String> concurrentHashMap = new ConcurrentHashMap<>();
        concurrentHashMap.put("pageIndex",String.valueOf(pageIndex));
        concurrentHashMap.put("pageSize",String.valueOf(pageSize));
        OkhttpCommonClient.sentPostRequest(url, concurrentHashMap, new OkhttpCommonCallBack(AppVersonModel.class) {
            @Override
            public void onSuccess(Object obj) {

                List<AppVersonModel> list = (List<AppVersonModel>) obj;

                switch (mLoadingType) {
                    case normal:
                    case pullLoading:
                        PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel1 = new PullRecyclerMoreStatueModel();
                        pullRecyclerMoreStatueModel1.itemLayoutId=R.layout.item_app_verson_0layout;
                        pullRecyclerMoreStatueModel1.itemType=1;
                        mCommonVersionModelList.add(pullRecyclerMoreStatueModel1);
                        break;
                }

                for (int i = 0; i < list.size(); i++) {
                    PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = new PullRecyclerMoreStatueModel();
                    pullRecyclerMoreStatueModel.model = list.get(i);
                    if (i==0){
                        pullRecyclerMoreStatueModel.itemType=2;
                        pullRecyclerMoreStatueModel.itemLayoutId=R.layout.item_app_verson_1layout;
                    }else {
                        pullRecyclerMoreStatueModel.itemType=3;
                        pullRecyclerMoreStatueModel.itemLayoutId=R.layout.item_app_verson_2layout;
                    }
                    mCommonVersionModelList.add(pullRecyclerMoreStatueModel);
                }

                switch (mLoadingType) {
                    case normal:
                    case pullLoading:
                        mPullRecyclerViewUtils.setLoadingDataList(mCommonVersionModelList);
                        break;
                    case upLoading:
                        if (list.size()>0){
                            mPullRecyclerViewUtils.setLoadingDataList(mCommonVersionModelList);
                        }else {
                            mPullRecyclerViewUtils.setLoadingDataList(null);
                        }
                        break;
                }

            }

            @Override
            public void onFaile(Object obj) {
                mPullRecyclerViewUtils.setLoadingDataList(mCommonVersionModelList);
            }
        });
    }

    private LoadingType mLoadingType = LoadingType.normal;
    private enum LoadingType{
        normal,
        upLoading,
        pullLoading
    }
    private PullRecyclerViewLinserner mPullRecclerLinserner = new PullRecyclerViewLinserner() {
        @Override
        public void loadMoreData() {

            pageIndex++;
            mLoadingType = LoadingType.upLoading;
            loadingNetDataFunction();
        }

        @Override
        public void loadingRefresDataFunction() {
            mCommonVersionModelList.clear();
            pageIndex=0;
            mLoadingType = LoadingType.pullLoading;
            loadingNetDataFunction();
        }

        @Override
        public void setViewDatas(View itemView, int position, int itemType, Object object) {
            PullRecyclerMoreStatueModel moreStatueModel = (PullRecyclerMoreStatueModel) object;
            int itemType1 = moreStatueModel.itemType;
            if (itemType1==1){
                TextView currentVersonTextView = itemView.findViewById(R.id.tv_app_current_verson);
                Button updateButton = itemView.findViewById(R.id.bt_app_current_verson);
                currentVersonTextView.setText(AppUtils.getVersionName(mContext));
                int versionCode = AppUtils.getVersionCode(mContext);
                PullRecyclerMoreStatueModel pullRecyclerMoreStatueModel = mCommonVersionModelList.get(1);
                AppVersonModel appVersonModel = (AppVersonModel) pullRecyclerMoreStatueModel.model;
                int versionCode1 = Integer.valueOf(appVersonModel.getVersionCode());
                if (versionCode1>versionCode){
                    updateButton.setTextColor(Color.BLUE);
                    //有新版本
                    updateButton.setText("更新至最新版本");
                    updateButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent();
                            intent.setAction("android.intent.action.VIEW");
                            Uri content_url = Uri.parse("http://a.app.qq.com/o/simple.jsp?pkgname=com.androidlongs.androidstudy.application");
                            intent.setData(content_url);
                            startActivity(intent);

                        }
                    });
                }else{
                    //无新版本
                    updateButton.setTextColor(Color.parseColor("#747474"));
                    updateButton.setText("已是最新版本");
                    updateButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            ToastUtils.show("已是最新版本",mContext);
                        }
                    });
                }

            }else if (itemType1==2) {
                AppVersonModel appVersonModel = (AppVersonModel) moreStatueModel.model;
                TextView dateTextView = itemView.findViewById(R.id.tv_current_verson_date);
                dateTextView.setText(DateUtils.getInstance().formatTime(Long.valueOf(appVersonModel.getCreateTime()),DateUtils.DEFAULT_DATE_FORMAT2));

                TextView versonNameTextView = itemView.findViewById(R.id.tv_current_verson_name);
                versonNameTextView.setText("版本 "+appVersonModel.getVersionName());

                TextView versonContentTextView = itemView.findViewById(R.id.tv_current_verson_content);
                versonContentTextView.setText(""+appVersonModel.getContent());
            }else if (itemType1==3){
                LinearLayout versonHistoryLayout = itemView.findViewById(R.id.ll_app_verson_history);
                View versonHistoryView = itemView.findViewById(R.id.v_app_verson_history_div);
                if (position==2){
                    versonHistoryLayout.setVisibility(View.VISIBLE);
                    versonHistoryView.setVisibility(View.VISIBLE);
                }else {
                    versonHistoryLayout.setVisibility(View.GONE);
                    versonHistoryView.setVisibility(View.GONE);
                }

                AppVersonModel appVersonModel = (AppVersonModel) moreStatueModel.model;
                TextView dateTextView = itemView.findViewById(R.id.tv_app_verson_history_date);
                dateTextView.setText(DateUtils.getInstance().formatTime(Long.valueOf(appVersonModel.getCreateTime()),DateUtils.DEFAULT_DATE_FORMAT2));

                TextView versonNameTextView = itemView.findViewById(R.id.tv_app_verson_history_name);
                versonNameTextView.setText(""+appVersonModel.getVersionName());

                TextView versonContentTextView = itemView.findViewById(R.id.tv_app_verson_history_content);
                versonContentTextView.setText(""+appVersonModel.getContent());

            }

        }
    };
    private PullRecyclerViewOnItemClickLinserner mItemClickLinserner = new PullRecyclerViewOnItemClickLinserner() {
        @Override
        public void setonItemClick(int position, int itemType, Object object) {

        }
    };
}
